﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bin.fontconv
{
    static class FontTool
    {
        public class Font
        {
            public string name;
            public int height;
            public int width;
            public Dictionary<char, byte[]> graydata;
        }

        static Dictionary<UInt16, char> mapGB2Utf16 = new Dictionary<ushort, char>();
        public static void LoadGB2Utf16(System.IO.Stream stream)
        {

            byte[] mapdata = new byte[stream.Length];
            stream.Read(mapdata, 0, mapdata.Length);

            for (var i = 0; i < mapdata.Length / 4; i++)
            {
                var key = (UInt16)(mapdata[i * 4 + 0] + mapdata[i * 4 + 1] * 256);
                var value = (char)(mapdata[i * 4 + 2] + mapdata[i * 4 + 3] * 256);
                mapGB2Utf16[key] = value;
            }
        }
        public static Font LoadAscII(byte size, string fontname, System.IO.Stream stream)
        {
            Font font = new Font();
            font.name = fontname;
            font.height = size;
            font.width = size / 2;
            font.graydata = new Dictionary<char, byte[]>();

            byte[] data = new byte[stream.Length];
            stream.Read(data, 0, data.Length);

            //byte[] data = System.IO.File.ReadAllBytes(filename);

            byte halfsize = (byte)(size / 2);

            int bytewidth = halfsize / 8;
            if (halfsize % 8 > 0)
                bytewidth++;

           

            int datablocksize = size * bytewidth;

            for (var i = 32; i < 127; i++)//ascii key range
            {
                var cdata = new byte[size * halfsize];
                Array.Fill<byte>(cdata, 0);
                char c = (char)i;
                //TileInfo charinfo;
                int pixeloffsetX = int.MaxValue;
                int pixeloffsetY = int.MaxValue;
                //charinfo.pixeloffsetX = int.MaxValue;
                //charinfo.pixeloffsetY = int.MaxValue;
                int pixeloffsetXMax = int.MinValue;
                int pixeloffsetYMax = int.MinValue;

                bool empty = true;
                for (var y = 0; y < size; y++)
                {
                    var bit = data[(i - 32) * datablocksize + y];
                    if (bit == 0) continue;
                    for (var x = 0; x < 8; x++)
                    {
                        if (x >= halfsize) continue;
                        var mask = 1 << (7 - x);

                        if ((bit & mask) > 0)
                        {
                            cdata[y * halfsize + x] = 255;

                            if (pixeloffsetX > x)
                                pixeloffsetX = x;

                            if (pixeloffsetY > y)
                                pixeloffsetY = y;

                            if (pixeloffsetXMax < x) pixeloffsetXMax = x;
                            if (pixeloffsetYMax < y) pixeloffsetYMax = y;
                            empty = false;

                        }
                    }
                }
                if (empty)
                {
                    //空的还管他个屁
                    continue;
                }
                else
                {
                    font.graydata[c] = cdata;
                    //int pixelSizeX = pixeloffsetXMax + 1 - pixeloffsetX;
                    //int pixelSizeY = pixeloffsetYMax + 1 - pixeloffsetY;
                    //var b = texture.AddSprite(halfsize, size, cdata, true, true, out Sprite s);
                    //// var s = texture.UpdateSprite_AutoCut(c.ToString(), halfsize, size, cdata);
                    //if (b)
                    //    font.sprites[c] = s;
                    ////var s = texture.UpdateSprite_Cut(c.ToString(), halfsize, size, cdata, (byte)pixeloffsetX, (byte)pixeloffsetY, pixelSizeX, pixelSizeY);
                }

                //bool b = texture.GetFreeTile(out charinfo.tileX, out charinfo.tileY);
                //if (!b) throw new Exception("TiledTexture is Full");
                //texture.UploadTile(charinfo.tileX, charinfo.tileY, cdata);
                //font.charTable[c.ToString()] = charinfo;
            }

            return font;
        }
        public static Font LoadQuWei(int size, string fontname, System.IO.Stream stream)
        {
            Font font = new Font();
            font.name = fontname;
            font.height = font.width = size;
            font.graydata = new Dictionary<char, byte[]>();

            byte[] data = new byte[stream.Length];
            stream.Read(data, 0, data.Length);



            int bytewidth = size / 8;
            if (size % 8 > 0)
                bytewidth++;

          

            int datablocksize = size * bytewidth;

            for (var iQu = 0; iQu < 94; iQu++)
            {
                for (var iWei = 0; iWei < 94; iWei++)
                {
                    var cdata = new byte[size * size];
                    Array.Fill<byte>(cdata, 0);
                    var key = (ushort)((0xa1 + iWei) * 256 + (0xa1 + iQu));
                    if (!mapGB2Utf16.TryGetValue(key, out char c))
                    {
                        continue;
                    }
                    int pixeloffsetX = int.MaxValue;
                    int pixeloffsetY = int.MaxValue;
                    int pixeloffsetXMax = int.MinValue;
                    int pixeloffsetYMax = int.MinValue;

                    bool empty = true;

                    var istart = (iQu * 94 + iWei) * datablocksize;//从这里开始
                    for (var y = 0; y < size; y++)
                    {
                        var bit1 = data[istart + y * 2];
                        var bit2 = data[istart + y * 2 + 1];
                        for (var x = 0; x < 16; x++)
                        {
                            bool yes = x < 8 ? ((bit1 & 1 << (7 - x)) > 0) : ((bit2 & 1 << (15 - x)) > 0);
                            if (yes)
                            {
                                cdata[y * size + x] = 255;

                                if (pixeloffsetX > x)
                                    pixeloffsetX = x;

                                if (pixeloffsetY > y)
                                    pixeloffsetY = y;

                                if (pixeloffsetXMax < x) pixeloffsetXMax = x;
                                if (pixeloffsetYMax < y) pixeloffsetYMax = y;
                                empty = false;
                            }
                        }
                    }

                    if (empty)
                    {
                        //空的还管他个屁
                        continue;
                    }
                    else
                    {
                        if (c > 65280 && c < 65375) //ascii码
                        {
                            char ascii = (char)(c - 65248);
                            font.graydata[ascii] = cdata;
                        }
                        else
                        {
                            font.graydata[c] = cdata;
                        }
                        //var b = texture.AddSprite((ushort)size, (ushort)size, cdata, true, true, out Sprite s);
                        //// var s = texture.UpdateSprite_AutoCut(c.ToString(), (byte)size, (byte)size, cdata);
                        //if (b)
                        //{
                        //    font.sprites[c] = s;

                        //    //ASCII转换
                        //    if (c == 12288)//空格,不可能发生的事情
                        //    {
                        //        font.sprites[c] = s;
                        //    }
                        //    else if (c > 65280 && c < 65375) //ascii码
                        //    {
                        //        char ascii = (char)(c - 65248);

                        //        //直接调整好,后面用的时候简单
                        //        var ascsprite = s;
                        //        ascsprite.borderX = 0;
                        //        ascsprite.borderY++;
                        //        ascsprite.width = (byte)(ascsprite.umax - ascsprite.umin + 1);
                        //        font.sprites[ascii] = ascsprite;
                        //    }
                        //}
                    }




                }
            }

            return font;
        }
    }
}
